package com.tj.ocr.similar;

import java.awt.image.BufferedImage;

public class Histogram {
	int greenBins = 151;
	int blueBins = 28;
	int redBins = 77;

	public void setGreenBinCount(int greenBinCount) {
		this.greenBins = greenBinCount;
	}

	public void setBlueBinCount(int blueBinCount) {
		this.blueBins = blueBinCount;
	}

	@SuppressWarnings("unused")
    public float[] filter(BufferedImage src, BufferedImage dest) {
		int width = src.getWidth();
		int height = src.getHeight();

		int[] inPixels = new int[width * height];
		float[] histogramData = new float[redBins * greenBins * blueBins];
		getRGB(src, 0, 0, width, height, inPixels);
		int index = 0;
		int redIdx = 0, greenIdx = 0, blueIdx = 0;
		int singleIndex = 0;
		float total = 0;
		for (int row = 0; row < height; row++) {
			int ta = 0, tr = 0, tg = 0, tb = 0;
			for (int col = 0; col < width; col++) {
				index = row * width + col;
				ta = (inPixels[index]) & 0xff000000;
				tr = (inPixels[index] >> 16) & 0xff;
				tg = (inPixels[index] >> 8) & 0xff;
				tb = inPixels[index] & 0xff;
				int gray = (tr * 77 + tg * 151 + tb * 28) >> 8;
				
				histogramData[singleIndex] += 1;
				total += 1;
			}
		}

		// start to normalize the histogram data
		for (int i = 0; i < histogramData.length; i++) {
			histogramData[i] = histogramData[i] / total;
		}

		return histogramData;
	}

	private void getRGB(BufferedImage src, int fromx, int fromy, int width, int height, int[] inPixels) {
		src.getRGB(fromx, fromy, width, height, inPixels, 0, 1);
	}
}
